//
//  WYHXNetWorkManager.m
//  WYHXGameProject
//
//  Created by 常金城 on 2018/6/4.
//  Copyright © 2018年 liubo. All rights reserved.
//

#import "WYHXNetWorkManager.h"
#import "NSObject+Hint.h"
#import "LBNavigationController.h"
@implementation WYHXNetWorkManager

/**
  设置单例
 */
+ (AFHTTPSessionManager *)defaultAFManager {
    static AFHTTPSessionManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [AFHTTPSessionManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript", @"text/plain", nil];
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        manager.requestSerializer.timeoutInterval = 20;
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        
    });
    return manager;
}
+ (id)get:(NSString *)path params:(NSDictionary *)params completionHandler:(void (^)(id, NSError *))completionHandler {
//    NSLog(@"NetPath: %@, NetParams: %@", path, params);
    return [[self defaultAFManager]GET:path parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        completionHandler(responseObject, nil);
//        NSString *code = responseObject[@"code"];
//        if ([code integerValue]==401) {
//            UIViewController *root = [UIApplication sharedApplication].keyWindow.rootViewController;
//            LBNavigationController *nav = (LBNavigationController *)root.presentedViewController;
//            LBLoginVC *loginVC = [[LBLoginVC alloc] init];
//            loginVC.jumpType = @"1";
//            [nav pushViewController:loginVC animated:YES];
//        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        [self handleError:error];
        completionHandler(nil, error);
    }];
}
+ (id)post:(NSString *)path params:(NSDictionary *)params completionHandler:(void (^)(id, NSError *))completionHandler {
//    NSLog(@"path: %@, params: %@", path, params);
    return [[self defaultAFManager]POST:path parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        completionHandler(responseObject, nil);
//        NSString *code = responseObject[@"code"];
//        if ([code integerValue]==401) {
//            UIViewController *root = [UIApplication sharedApplication].keyWindow.rootViewController;
//            LBNavigationController *nav = (LBNavigationController *)root.presentedViewController;
//            LBLoginVC *loginVC = [[LBLoginVC alloc] init];
//            loginVC.jumpType = @"1";
//            [nav pushViewController:loginVC animated:YES];
//        }

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        [self handleError:error];
        completionHandler(nil, error);
    }];
    
}
//- (void)jumpLloginviewController{
//    LBLoginVC *loginVC = [[LBLoginVC alloc] init];
//    loginVC.jumpType = @"1";
//    [self.navigationController pushViewController:loginVC animated:YES];
//}
// 上传图片
+(void)uploadSinglePhotoWith:(NSString *)path params:(NSDictionary *)params WithDataArray:(NSMutableArray *)dataArray withParamsArray:(NSMutableArray *)fileParams AndFinish:(void (^)(NSDictionary *, NSError *))completionHandler
{
    
    AFHTTPSessionManager *manager = [self defaultAFManager];
    //2. 发起网络请求
    
    [manager POST:path parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        
        for (int i = 0 ; i < fileParams.count; i++) {
            NSString *fileName = [NSString stringWithFormat:@"%@%d.jpeg",str,i];
            [formData appendPartWithFileData:dataArray[i] name:fileParams[i] fileName:fileName mimeType:@"image/jpeg"];
        }
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        completionHandler(responseObject, nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        completionHandler(nil, error);
        
    }];
}
/**
 提示语

 @param error 错误提示
 */
+ (void)handleError:(NSError *)error  {
    //弹出错误信息
    [[self new] showErrorWithMsg:error];
}

@end
